package com.xiaomeng.principle.ocp;

public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}
// 绘图类
class GraphicEditor {
    public void drawShape(Shape s) {
        if (s.m_ype == 1) {
            drawRectangle(s);
        } else if (s.m_ype == 2) {
            drawCircle(s);
        } else if (s.m_ype == 3) {
            drawTriangle(s);
        }

    }
    // 绘制矩形
    public void drawRectangle(Shape s) {
        System.out.println("绘制矩形");
    }
    // 绘制圆形
    public void drawCircle(Shape s) {
        System.out.println("绘制圆形");
    }
    // 绘制三角形
    public void drawTriangle(Shape s) {
        System.out.println("绘制三角形");
    }
}
// Shape类 基类
class Shape {
    int m_ype;
}
class Rectangle extends Shape {
    Rectangle() {
        super.m_ype = 1;
    }
}
class Circle extends Shape {
    Circle() {
        super.m_ype = 2;
    }
}
// 新增画三角形
class Triangle extends Shape {
    Triangle() {
        super.m_ype = 3;
    }
}